package com.fingard.dsp.bank.directbank.chinapay03;

import java.io.File;
import java.util.Calendar;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.ITransaction;
import com.fingard.dsp.bank.reqrespobj.transaction.Req7515TodayTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.Req7516HisTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.RetAtsTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.detail.RetAtsTransDetail;

public class ChinaPay03Transaction extends ChinaPay03Base implements ITransaction {
    @Override
    public RetAtsTrans d_7516_HisTrans(Req7516HisTrans p_req7516) throws Exception {
        //历史明细查询返回ATS的对象
        RetAtsTrans retAts = new RetAtsTrans(p_req7516.reqHeader);
        String tmpRespCode = "";
        String tmpRespInfo = "";
        retAts.writeStart();//写返回ATS的统一头部信息
        try {
            Calendar tmpCalendarFrom = Calendar.getInstance();
            tmpCalendarFrom.setTime(p_req7516.getDateFrom());

            Calendar tmpCalendarTo = Calendar.getInstance();
            tmpCalendarTo.setTime(p_req7516.getDateTo());

            int tmpIntDateFrom = 0;
            int tmpIntDateTo = 0;
            //按自然日循环查询
            while (tmpIntDateFrom <= tmpIntDateTo && tmpRespCode.length() == 0) {
                Calendar tmpClndrStepTo = Calendar.getInstance();
                tmpClndrStepTo.setTime(tmpCalendarFrom.getTime());

                if("康宁医院".equals(getBankConfig().getValueAsString("groupName"))){
                    String fileName = statementFilePath + Format.Date8Format.format(tmpCalendarFrom.getTime()) + File.separator + getBankConfig().getValueAsString("groupID") + "_" + Format.Date8Format.format(tmpCalendarFrom.getTime());
                    analysis_68_kn(fileName+ "_68.xls",p_req7516.actNum,retAts);
                    analysis_1_kn(fileName+ "_1.xls",p_req7516.actNum,retAts);
                    analysis_9999_kn(fileName+ "_9999.xls",p_req7516.actNum,retAts);
                    analysis_86_kn(fileName+ "_86.xls",p_req7516.actNum,retAts);
                }else {
                    //region 读取68文件
                    String fileName = statementFilePath + Format.Date8Format.format(tmpCalendarFrom.getTime()) + File.separator + getBankConfig().getValueAsString("groupID") + "_" + Format.Date8Format.format(tmpCalendarFrom.getTime()) + "_68.xls";
                    WriteBankLogLn("开始读取文件：" + fileName);
                    String fileContent = readExcel(fileName);
                    WriteBankLogLn2("对账单重组后响应内容为:\r\n" + fileContent);
                    String[] totalLine = fileContent.split("\r\n");
                    for (String eachline : totalLine) {
                        if("东航物流".equals(getBankConfig().getValueAsString("groupName"))){
                            //0.商户号 1.交易日期 2.交易时间 3.终端号 4.交易类型 5.卡号 6.交易金额 7.清算金额
                            //8.手续费 9.参考号 10.流水号 11.商户名称 12.卡类型 13.账单号 14.支付方式
                            //15.发卡行 16.易pos序列号 17.银商订单号 18.商户订单号 19.收银员编号 20.备注字段 21.分店简称
                            String[] eachNode = eachline.split(",");
                            if (eachNode.length > 1 && eachNode[1].length() > 10) {
                                if (p_req7516.actNum.equals(eachNode[0])) {
                                    RetAtsTransDetail tmpRetDetail = new RetAtsTransDetail();
                                    tmpRetDetail.actNum = p_req7516.actNum;
                                    tmpRetDetail.tranDate = eachNode[1].substring(0, 10);
                                    tmpRetDetail.valueDate = tmpRetDetail.tranDate;
                                    tmpRetDetail.tranTime = eachNode[1].substring(0, 10) + " " + eachNode[2];
                                    tmpRetDetail.recAccNum = eachNode[5];
                                    tmpRetDetail.recAccBank = eachNode[15];
                                    if (eachNode[6].contains("-")) {
                                        tmpRetDetail.cdFlag = "1";
                                    } else {
                                        tmpRetDetail.cdFlag = "2";
                                    }
                                    tmpRetDetail.tranAmt = eachNode[6].replace("-", "");
                                    tmpRetDetail.askNumber = eachNode[17];
                                    tmpRetDetail.payCode = eachNode[21];
                                    tmpRetDetail.originator = eachNode[9];
                                    tmpRetDetail.busType = eachNode[14];
                                    tmpRetDetail.fee = eachNode[8];
                                    tmpRetDetail.urid = eachNode[17];
                                    tmpRetDetail.possCode=eachNode[3];//终端号
                                    retAts.writeRetDetail(tmpRetDetail);
                                }
                            }
                        }else if("海明控股".equals(getBankConfig().getValueAsString("groupName"))){
//                        0.清算日期 1.支付方式	2.商户编号 3.商户名称 4.终端号 5.分店简称 6.交易类型 7.卡号
//                        8.交易金额 9.商户优惠金额 10.钱包优惠金额	11.手续费 12.清算金额 13.交易日期
//                        14.交易时间 15.发卡行	16.参考号 17.担保号	18.卡类型 19.银商订单号	20.备注
                            String[] eachNode = eachline.split(",");
                            if (eachNode.length > 13 && eachNode[13].length() > 9) {
                                if (p_req7516.actNum.equals(eachNode[2])) {
                                    RetAtsTransDetail tmpRetDetail = new RetAtsTransDetail();
                                    tmpRetDetail.actNum = p_req7516.actNum;
                                    tmpRetDetail.tranDate = eachNode[13].substring(0, 10);
                                    tmpRetDetail.valueDate = tmpRetDetail.tranDate;
                                    tmpRetDetail.tranTime = tmpRetDetail.tranDate + " " + eachNode[14];
                                    tmpRetDetail.recAccNum = eachNode[7];
                                    tmpRetDetail.recAccBank = eachNode[15];
                                    if (eachNode[8].contains("-")) {
                                        tmpRetDetail.cdFlag = "1";
                                    } else {
                                        tmpRetDetail.cdFlag = "2";
                                    }
                                    tmpRetDetail.tranAmt = eachNode[8];
                                    tmpRetDetail.askNumber = eachNode[19];
                                    tmpRetDetail.payCode = eachNode[16];
                                    tmpRetDetail.originator = eachNode[2];
                                    tmpRetDetail.busType = eachNode[1];
                                    tmpRetDetail.fee = eachNode[11];
                                    tmpRetDetail.urid = eachNode[19];
                                    tmpRetDetail.possCode=eachNode[4];//终端号
                                    retAts.writeRetDetail(tmpRetDetail);
                                }
                            }
                        }else {//适用万国击剑
                            //0.商户号 1.交易日期 2.交易时间 3.终端号 4.交易类型 5.卡号 6.交易金额 7.清算金额 8.手续费 9.参考号 10.流水号 11.卡类型 12.支付方式 13.发卡行 14.分店名称 15.唯一参考号
                            String[] eachNode = eachline.split(",");
                            if (eachNode.length > 1 && eachNode[1].length() > 10) {
                                if (p_req7516.actNum.equals(eachNode[0])) {
                                    RetAtsTransDetail tmpRetDetail = new RetAtsTransDetail();
                                    tmpRetDetail.actNum = p_req7516.actNum;
                                    tmpRetDetail.tranDate = eachNode[1].substring(0, 10);
                                    tmpRetDetail.valueDate = tmpRetDetail.tranDate;
                                    tmpRetDetail.tranTime = eachNode[1].substring(0, 10) + " " + eachNode[2];
                                    tmpRetDetail.recAccNum = eachNode[5];
                                    tmpRetDetail.recAccBank = eachNode[13];
                                    if (eachNode[6].contains("-")) {
                                        tmpRetDetail.cdFlag = "1";
                                    } else {
                                        tmpRetDetail.cdFlag = "2";
                                    }
                                    tmpRetDetail.tranAmt = eachNode[6].replace("-", "");
                                    tmpRetDetail.askNumber = eachNode.length >= 16 ? eachNode[15] : "";
                                    tmpRetDetail.payCode = eachNode[14];
                                    tmpRetDetail.originator = eachNode[9];
                                    tmpRetDetail.busType = eachNode[12];
                                    tmpRetDetail.fee = eachNode[8];
                                    tmpRetDetail.urid = eachNode.length >= 16 ? eachNode[15] : "";
                                    tmpRetDetail.possCode=eachNode[3];//终端号
                                    retAts.writeRetDetail(tmpRetDetail);
                                }
                            }
                        }
                    }
                    //endregion

                    //region 读取47文件
                    String fileName47 = statementFilePath + Format.Date8Format.format(tmpCalendarFrom.getTime()) + File.separator + getBankConfig().getValueAsString("groupID") + "_" + Format.Date8Format.format(tmpCalendarFrom.getTime()) + "_47.xls";
                    WriteBankLogLn("开始读取文件：" + fileName47);
                    String fileContent47 = readExcel(fileName47);
                    WriteBankLogLn2("对账单重组后响应内容为:\r\n" + fileContent47);
                    String[] totalLine47 = fileContent47.split("\r\n");
                    for (String eachline : totalLine47) {
                        //0.商户名称 1.交易日期 2.交易时间 3.商户号 4.终端号 5.卡号 6.交易类型 7.交易金额 8.清算金额 9.手续费 10.流水号 11.检索参考号 12.卡类型 13.发卡行 14.唯一参考号

                        String[] eachNode = eachline.split(",");
                        if (eachNode.length > 1 && eachNode[1].length() > 10) {
                            if (p_req7516.actNum.equals(eachNode[3])) {
                                RetAtsTransDetail tmpRetDetail = new RetAtsTransDetail();
                                tmpRetDetail.actNum = p_req7516.actNum;
                                tmpRetDetail.tranDate = eachNode[1].substring(0, 10);
                                tmpRetDetail.valueDate = tmpRetDetail.tranDate;
                                tmpRetDetail.tranTime = eachNode[1].substring(0, 10) + " " + eachNode[2];
                                tmpRetDetail.recAccNum = eachNode[5];
                                tmpRetDetail.recAccBank = eachNode[13];
                                if (eachNode[7].contains("-")) {
                                    tmpRetDetail.cdFlag = "1";
                                } else {
                                    tmpRetDetail.cdFlag = "2";
                                }
                                tmpRetDetail.tranAmt = eachNode[7].replace("-", "");
                                tmpRetDetail.askNumber = eachNode.length >= 15 ? eachNode[14] : "";
                                tmpRetDetail.originator = eachNode[11];
                                tmpRetDetail.busType = eachNode[6];
                                tmpRetDetail.fee = eachNode[9];
                                tmpRetDetail.urid = eachNode.length >= 15 ? eachNode[14] : "";
                                tmpRetDetail.possCode=eachNode[4];//终端号
                                retAts.writeRetDetail(tmpRetDetail);
                            }
                        }
                    }
                    //endregion

                    //region 读取1文件
                    String fileName1 = statementFilePath + Format.Date8Format.format(tmpCalendarFrom.getTime()) + File.separator + getBankConfig().getValueAsString("groupID") + "_" + Format.Date8Format.format(tmpCalendarFrom.getTime()) + "_1.xls";
                    WriteBankLogLn("开始读取文件：" + fileName1);
                    String fileContent1 = readExcel(fileName1);
                    WriteBankLogLn2("对账单重组后响应内容为:\r\n" + fileContent1);
                    String[] totalLine1 = fileContent1.split("\r\n");
                    for (String eachline : totalLine1) {
                        if("海明控股".equals(getBankConfig().getValueAsString("groupName"))){
//                       0.清算日期	1.商户编号 2.商户名称 3.终端号 4.分店简称 5.交易类型 6.卡号	7.交易金额
//                       8.手续费 9.清算金额 10.交易日期 11.交易时间 12.发卡行 13.参考号 14.授权码	15.卡类型	16.备注
                            String[] eachNode = eachline.split(",");
                            if (eachNode.length > 11 && eachNode[11].length() > 9) {
                                if (p_req7516.actNum.equals(eachNode[0])) {
                                    RetAtsTransDetail tmpRetDetail = new RetAtsTransDetail();
                                    tmpRetDetail.actNum = p_req7516.actNum;
                                    tmpRetDetail.tranDate = eachNode[10].substring(0, 10);
                                    tmpRetDetail.valueDate = tmpRetDetail.tranDate;
                                    tmpRetDetail.tranTime = tmpRetDetail.tranDate + " " + eachNode[11];
                                    tmpRetDetail.recAccNum = eachNode[6];
                                    tmpRetDetail.recAccBank = eachNode[12];
                                    if (eachNode[7].contains("-")) {
                                        tmpRetDetail.cdFlag = "1";
                                    } else {
                                        tmpRetDetail.cdFlag = "2";
                                    }
                                    tmpRetDetail.tranAmt = eachNode[7].replace("-", "");
                                    tmpRetDetail.askNumber = "";
                                    //tmpRetDetail.payCode = eachNode[11];
                                    tmpRetDetail.originator = eachNode[1];
                                    //tmpRetDetail.busType = eachNode[12];//?
                                    tmpRetDetail.fee = eachNode[8];
                                    tmpRetDetail.urid = "";
                                    tmpRetDetail.possCode = eachNode[3];//终端号
                                    retAts.writeRetDetail(tmpRetDetail);
                                }
                            }
                        }else {
                            //0.商户号 1.交易日期 2.交易时间 3.终端号 4.交易类型 5.卡号 6.交易金额 7.清算金额
                            //8.手续费 9.参考号 10.流水号 11.商户名称 12.卡类型 13.发卡行
                            String[] eachNode = eachline.split(",");
                            if (eachNode.length > 1 && eachNode[1].length() > 10) {
                                if (p_req7516.actNum.equals(eachNode[0])) {
                                    RetAtsTransDetail tmpRetDetail = new RetAtsTransDetail();
                                    tmpRetDetail.actNum = p_req7516.actNum;
                                    tmpRetDetail.tranDate = eachNode[1].substring(0, 10);
                                    tmpRetDetail.valueDate = tmpRetDetail.tranDate;
                                    tmpRetDetail.tranTime = eachNode[1].substring(0, 10) + " " + eachNode[2];
                                    tmpRetDetail.recAccNum = eachNode[5];
                                    tmpRetDetail.recAccBank = eachNode[13];
                                    if (eachNode[6].contains("-")) {
                                        tmpRetDetail.cdFlag = "1";
                                    } else {
                                        tmpRetDetail.cdFlag = "2";
                                    }
                                    tmpRetDetail.tranAmt = eachNode[6].replace("-", "");
                                    tmpRetDetail.askNumber = "";
                                    //tmpRetDetail.payCode = eachNode[11];
                                    tmpRetDetail.originator = eachNode[9];
                                    //tmpRetDetail.busType = eachNode[12];//?
                                    tmpRetDetail.fee = eachNode[8];
                                    tmpRetDetail.urid = "";
                                    tmpRetDetail.possCode = eachNode[3];//终端号
                                    retAts.writeRetDetail(tmpRetDetail);
                                }
                            }
                        }
                    }
                    //endregion

                    //region 读取9文件
                    String fileName9 = statementFilePath + Format.Date8Format.format(tmpCalendarFrom.getTime()) + File.separator + getBankConfig().getValueAsString("groupID") + "_" + Format.Date8Format.format(tmpCalendarFrom.getTime()) + "_9.xls";
                    WriteBankLogLn("开始读取文件：" + fileName9);
                    String fileContent9 = readExcel(fileName9);
                    WriteBankLogLn2("对账单重组后响应内容为:\r\n" + fileContent9);
                    String[] totalLine9 = fileContent9.split("\r\n");
                    for (String eachline : totalLine9) {
                        //0.商户号 1.交易日期 2.交易时间 3.交易类型 4.卡号 5.交易金额 6.终端号 7.清算金额
                        //8.手续费 9.参考号 10 流水号 11.商户名称 12.卡类型 13.商户订单号 14.支付方式 15.银商订单号
                        // 16.退货订单号 17.实际支付金额 18.备注 19.付款附言 20.钱包优惠金额 21.商户优惠金额 22.发卡行 23.分店名称
                        String[] eachNode = eachline.split(",");
                        if (eachNode.length > 1 && eachNode[1].length() > 10) {
                            if (p_req7516.actNum.equals(eachNode[0])) {
                                RetAtsTransDetail tmpRetDetail = new RetAtsTransDetail();
                                tmpRetDetail.actNum = p_req7516.actNum;
                                tmpRetDetail.tranDate = eachNode[1].substring(0, 10);
                                tmpRetDetail.valueDate = tmpRetDetail.tranDate;
                                tmpRetDetail.tranTime = eachNode[1].substring(0, 10) + " " + eachNode[2];
                                tmpRetDetail.recAccNum = eachNode[4];
                                tmpRetDetail.recAccBank = eachNode[22];
                                if (eachNode[5].contains("-")) {
                                    tmpRetDetail.cdFlag = "1";
                                } else {
                                    tmpRetDetail.cdFlag = "2";
                                }
                                tmpRetDetail.tranAmt = eachNode[5].replace("-", "");
                                tmpRetDetail.askNumber = eachNode[13];
                                if(eachNode.length<24){
                                    tmpRetDetail.payCode="";
                                }else {
                                    tmpRetDetail.payCode = eachNode[23];
                                }
                                tmpRetDetail.originator = eachNode[9];
                                tmpRetDetail.busType = eachNode[14];
                                tmpRetDetail.fee = eachNode[8];
                                tmpRetDetail.urid = eachNode[13];
                                tmpRetDetail.possCode=eachNode[6];//终端号
                                retAts.writeRetDetail(tmpRetDetail);
                            }
                        }
                    }
                }
                //endregion

                tmpCalendarFrom = tmpClndrStepTo;
                tmpCalendarFrom.add(Calendar.DATE, 1);
                tmpIntDateFrom = Integer.parseInt(Format.Date8Format.format(tmpCalendarFrom.getTime()));
                tmpIntDateTo = Integer.parseInt(Format.Date8Format.format(tmpCalendarTo.getTime()));
            }
        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        if (tmpRespCode.equals("-1")) {
            retAts = new RetAtsTrans(p_req7516.reqHeader);
            retAts.writeStart();//写返回ATS的统一头部信息
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("交易为空");
        }
        retAts.writeEnd();
        return retAts;
    }


    @Override
    public RetAtsTrans d_7515_TodayTrans(Req7515TodayTrans p_req7515) throws Exception {
        return null;
    }
}
